﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPay.KooBoo.Business.Interfaces;
using VietPay.KooBoo.Business;
using VietPay.KooBoo.Business.Models;

namespace Kooboo.CMS.Modules.Areas.NewsModule.Controllers
{
    public class ProductController : Controller
    {
        private IProductService _service;

        public ProductController()
            : this(new ProductService())
        {
        }

        public ProductController(IProductService pdservice)
        {
            _service = pdservice;
        }

        //
        // GET: /NewsModule/Category/

        public ActionResult Index()
        {
            return View(_service.GetProductList(10, 10));
        }

        //
        // GET: /NewsModule/Category/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /NewsModule/Category/Create

        public ActionResult Create()
        {
            var vViewModel = new ProductViewModel();
            return View(vViewModel);
        } 

        //
        // POST: /NewsModule/Category/Create

        [HttpPost]
        public ActionResult Create(ProductViewModel vViewModel)
        {
            try
            {
                // TODO: Add insert logic here
                _service.Add(vViewModel);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /NewsModule/Category/Edit/5
 
        public ActionResult Edit(int id=-1)
        {
            var vViewModel = _service.Query(p => p.ProductID == id).FirstOrDefault();
            if (vViewModel == null)
            {
                return HttpNotFound();
            }
            return View(vViewModel);
        }

        //
        // POST: /NewsModule/Category/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, ProductViewModel vViewModel)
        {
            try
            {
                // TODO: Add update logic here
                vViewModel.ProductID = id;
                _service.Update(vViewModel);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /NewsModule/Category/Delete/5
 
        public ActionResult Delete(int id=-1)
        {
            var vViewModel = _service.Query(p => p.ProductID == id).FirstOrDefault();
            if (vViewModel == null)
            {
                return HttpNotFound();
            }
            return View(vViewModel);
        }

        //
        // POST: /NewsModule/Category/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, ProductViewModel vViewModel)
        {
            try
            {
                // TODO: Add delete logic here
                vViewModel.ProductID = id;
                _service.Delete(vViewModel);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
